package de.ret_world.openrpgstudio.editor;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 * The MediaManager manages all your Media information. Get an instance through the created Project.
 *
 * @author Emu
 */
public class MediaManager {

    private HashMap<String, Media> mediaMap;

    protected MediaManager() {
        mediaMap = new HashMap<>();
    }

    /**
     * @param name Name of the media to get
     * @return the Media object with the given name
     */
    public Media getMedia(String name) {
        return mediaMap.get(name);
    }

    /**
     * @param media adds the media to the manager (Attention: you can't have two media objects with the same name!)
     */
    public void addMedia(Media media) {
        mediaMap.put(media.getID(), media);
    }

    /**
     * @param media Removes this media from the manager
     */
    public void removeMedia(Media media) {
        removeMedia(media.getID());
    }

    /**
     * @param name Removes the media with this name from the manager
     */
    public void removeMedia(String name) {
        mediaMap.remove(name);
    }

    /**
     * @return the inner map to store all media. use with care!
     */
    public HashMap<String, Media> getMediaMap() {
        return mediaMap;
    }

    protected void storeMediaManager() throws IOException {
        Properties scriptP = new Properties();
        for (String k : mediaMap.keySet()) {
            Media m = mediaMap.get(k);
            String out = "[" + m.getType() + ", " + m.getFile() + ", {";

            if (m.getUsers().size() > 0) {
                for (String s : m.getUsers()) {
                    out += s + "; ";
                }
                out = out.substring(0, out.length() - 2);
            }

            out += "}]";

            scriptP.setProperty(k, out);

        }

        scriptP.store(Files.newOutputStream(Project.getInstance().getFolder("root").resolve("mediamanager.properties")), "OpenRPGStudio Data File");

        Logger.getLogger(Project.class).info("MediaManager saved successfully!");
    }

    protected void loadMediaManager() throws IOException {
        Properties prop = new Properties();
        prop.load(Files.newInputStream(Project.getInstance().getFolder("root").resolve("mediamanager.properties")));

        for (String k : prop.stringPropertyNames()) {
            String v = prop.getProperty(k);
            v = v.substring(1, v.length() - 1);
            String vals[] = v.split(",");
            Media m = new Media(Integer.parseInt(vals[0].trim()), k, Paths.get(vals[1].trim()));

            String users = vals[2].trim();
            users = users.substring(1, users.length() - 1);
            if (users.length() > 0) {
                String u[] = users.split(";");
                for (int i = 0; i < u.length; i++) {
                    m.getUsers().add(u[i].trim());
                }
            }
            mediaMap.put(k, m);
        }
        Logger.getLogger(Project.class).info("MediaManager loaded successfully!");
    }
}